package cn.genmer.test.security.writtenTest.alibaba;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: 整体结果思路其实就是用正则表达式，配合Java提供的Pattern和Matcher和对应方法完成的匹配和确认位置
 * @description:
 * @author: Genmer
 * @create: 2021-08-26 19:33
 **/
public class TextMatch {
    public static void main(String[] args) {
        // 比如原始文本是
        String text = "1、It’s better to be alone than to be with someone you’re not happy to be with. 宁愿一个人呆着，也不要跟不合拍的人呆一块。\n" +
                "\n" +
                "2、I lied when I said I didn’t like you. I lied when I said I didn’t care. I lie every time I try to tell myself I will never fall for you.我说不爱你，那是假话；我说不在乎，那是假话；我告诉自己对你再不会有感觉了，那也是假话。\n" +
                "\n" +
                "3、If they throw stones at you, don't throw back, use them to build your own foundation instead. 如果别人朝你扔石头，就不要扔回去了，留着作你建高楼的基石。\n" +
                "\n" +
                "英语（英文）经典句子\n" +
                "\n" +
                "4、Progress is the activity of today and the assurance of tomorrow . 进步是今天的活动、明天的保证。\n" +
                "\n" +
                "5、It’s better to be alone than to be with someone you’re not happy to be with.宁愿一个人呆着，（经典语句www.lzcn）也不要跟不合拍的人呆一块。\n" +
                "\n" +
                "6、Whatever is worth doing is worth doing well.任何值得做的，就把它做好。\n" +
                "\n" +
                "7、No matter how bad your heart has been broken, the world doesn’t stop for your grief. The sun comes right back up the next day.不管你有多痛苦，这个世界都不会为你停止转动。太阳依旧照样升起。\n" +
                "\n" +
                "8、Accept what was and what is, and you’ll have more positive energy to pursue what will be.接受过去和现在的模样，才会有能量去追寻自己的未来。\n" +
                "\n" +
                "9、Time goes by so fast, people go in and out of your life. You must never miss the opportunity to tell these people how much they mean to you.时间在流逝，生命中人来人往。不要错失机会，告诉他们在你生命中的意义。\n" +
                "\n" +
                "10、You may be out of my sight, but never out of my mind.你也许已走出我的视线，但从未走出我的思念。";
        List<String> words = new ArrayList<>();
        // 添加关键词
        words.add("didn’t");
        words.add("people");

        // 可断点查看
        Map<String, List<Integer>> result = searchKeywordInText(text, words);
        System.out.println(result);

    }

    /**
     * 根据关键词匹配文本
     * @param text 原始文本
     * @param keywords 关键词列表
     * @return
     */
    public static Map<String, List<Integer>> searchKeywordInText(String text, List<String> keywords) {
        //text是原始⽂本，keywords是关键字列表，输出结果key是关键字，value是命中的关键字在原始⽂本 中的位置
        // 定义结果Map
        Map<String, List<Integer>> resultMap = new HashMap<>();
        // 遍历关键词
        for (String key: keywords){
            // 执行匹配方法 - 返回对于匹配结果（无结果返回空列表）
            List<Integer> indexList = compileKeyWord(text, key);
            // 封装结果
            resultMap.put(key,indexList);
        }
        return resultMap;
    }

    /**
     * 关键字匹配 - 返回匹配地址（通过Pattern和Matcher使用正则完成）
     * @param word 内容
     * @param keyWord 关键字
     * @return
     */
    public static List<Integer> compileKeyWord(String word, String keyWord) {
        // String.format避免底层编译为多个StringBuffer
        Pattern pattern = Pattern.compile(String.format("(.*%s.*)", keyWord).toString());
        Matcher mt=pattern.matcher(word);
        // 构建关键词所在位置列表
        List indexList = new ArrayList();
        // 是否匹配成功，可匹配多次
        while(mt.find()){
            // 找到一次所在位置
            int index = mt.start();
            System.out.println(mt.group()+"  关键字所在："+ index);
            // 就保存一次地址
            indexList.add(index);
        }
        // 返回结果
        return indexList;
    }
}
